'use strict'

/**
 * 更新缓存
 * @param modContactList
 * @param wxSession
 */
function cacheContact(modContactList, wxSession) {
    modContactList.forEach(o => {
        // 群组直接替换
        if (o.UserName.startsWith('@@')) {
            wxSession.groupContact[o.userName] = {
                nickname: o.NickName,
                memberList: o.MemberList
            }
        } else { // 好友
            // 如果不在缓存中
            var index = wxSession.memberList.findIndex(user => user['UserName'] == o.UserName);
            if (index < 0) {
                // 新增
                wxSession.memberList.push(o);
            } else {
                // 更新
                wxSession.memberList[index] = o;
            }
        }
    })
}

exports.cacheContact = cacheContact;